/**
 * @file acwing/2171/main
 * @brief EK 求最大流
 * 给定一个包含 n 个点 m 条边的有向图，并给定每条边的容量，边的容量非负。
 * 图中可能存在重边和自环。
 * 求从点 S 到点 T 的最大流。
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/6/20 14:25:03
 **/

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1010, M = 20010, INF = 1e8;
int n, m, S, T;
int h[N], e[M], f[M], ne[M], idx;
int q[N], d[N], pre[N];
bool st[N];
void add(int a, int b, int c) {
  e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
  e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}

bool bfs() {
  int hh = 0, tt = 0;
  memset(st, false, sizeof(st));
  q[0] = S, st[S] = true, d[S] = INF;
  while (hh <= tt) {
    int t = q[hh++];
    for (int i = h[t]; ~i; i = ne[i]) {
      int ver = e[i];
      if (!st[ver] && f[i]) {
        st[ver] = true;
        d[ver] = min(d[t], f[i]);
        pre[ver] = i;
        if (ver == T) return true;
        q[++tt] = ver;
      }
    }
  }
  return false;
}

int EK() {
  int r = 0;
  while (bfs()) {
    r += d[T];
    for (int i = T; i != S; i = e[pre[i] ^ 1])
      f[pre[i]] -= d[T], f[pre[i] ^ 1] += d[T];
  }
  return r;
}

int main() {
  scanf("%d%d%d%d", &n, &m, &S, &T);
  memset(h, -1, sizeof(h));
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c);
  }
  printf("%d\n", EK());
  return 0;
}
